package com.media.publish

import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.ItemTouchHelper
import com.baselibrary.bean.ReleaseTopicBean
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.SpUtils
import com.media.databinding.ActivityReleaseImageVideoBinding
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import com.media.publish.adapter.ImageVideoAdapter
import java.util.*

/**
 * @author: 关忆北、
 * @date: 2022/5/17
 * @Des：
 */
class ReleaseDynamicViewModel : ViewModel() {
    var topicListLD: MutableLiveData<List<ReleaseTopicBean.DataBean>> = MutableLiveData()

    val mMediaPathList: ArrayList<String> = ArrayList()
    val mSDPathList = ArrayList<String>()
    lateinit var mBinding: ActivityReleaseImageVideoBinding

    companion object {
        const val REQUEST_LOCATION = 300 //位置选择
        const val REQUEST_IMAGEEDIT = 111
        const val RESULT_EDITIMGOVVER = 202
        const val RESULT_COVERS = 203 //跳转选择封面 响应码
        const val RESULT_QINIU = 0x101 //跳转七牛云
        const val REQUEST_QINIU = 0x102 //回执七牛云
        const val REQUEST_SREARCH_TOPIC = 0x105 //跳转搜索话题界面
        const val REQUEST_COVERS = 0x107 //跳转选择封面 请求码
        const val QINIUVIDEO_PATH = "QINIUVIDEO_PATH" //视频路径
        const val QINIUVIDEO_TIME = "QINIUVIDEO_TIME" //视频时长
        const val QINIUVCOVER_PATH = "QINIUVOCER_PATH" //封面路径
    }

    fun reqDefSearchTopics() {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqDefSearchTopics(it)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<ReleaseTopicBean.DataBean>>(this) {
                    override fun onSuccess(result: List<ReleaseTopicBean.DataBean>, json: String?) {
                        topicListLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        topicListLD.value = null
                    }

                })
        }
    }

    fun setItemHelper(mTouchHelper: ItemTouchHelper, mImageVideoAdapter: ImageVideoAdapter) {
        if (mMediaPathList.size > 1) {
            mTouchHelper.attachToRecyclerView(mBinding.rvImagevideo)
            mImageVideoAdapter.setItemHelper(mTouchHelper)
            mBinding.ivImageVideoDrag.visibility = if (SpUtils.getInstance().dynamicDrag) View.GONE else View.VISIBLE
        } else {
            mTouchHelper.attachToRecyclerView(null)
            mImageVideoAdapter.setItemHelper(null)
        }
    }

    /**
     * 获取更过赞指导
     */
    fun getMoreGiveGuide(mBinding: ActivityReleaseImageVideoBinding, list: List<String?>, isAddTopic: Boolean, dynamicType: Int): String? {
        if (dynamicType == 2) {
            mBinding.tvImageVideoMoreGive.visibility = View.INVISIBLE
            return ""
        }
        val buffer = StringBuilder()
        val writingBoolean: Boolean
        val pictureBoolean: Boolean
        val topicBoolean: Boolean
        if (mBinding.etImagevideoInput.text.toString().length < 30) {
            writingBoolean = false
            buffer.append("再写").append(30 - mBinding.etImagevideoInput.text.toString().length).append("字，")
        } else {
            writingBoolean = true
        }
        if (list.size < 4) {
            pictureBoolean = false
            buffer.append("添加").append(4 - list.size).append("张图，")
        } else {
            pictureBoolean = true
        }
        if (!isAddTopic) {
            topicBoolean = false
            buffer.append(if (pictureBoolean) "添加1个话题，" else "1个话题，")
        } else {
            topicBoolean = true
        }
        buffer.append("可获得更多赞")
        if (writingBoolean && pictureBoolean && topicBoolean) {
            mBinding.tvImageVideoMoreGive.visibility = View.INVISIBLE
        } else {
            mBinding.tvImageVideoMoreGive.visibility = View.VISIBLE
            mBinding.tvImageVideoMoreGive.text = buffer.toString()
        }
        return buffer.toString()
    }


}